﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YJKRoomInspection.Model;
using YJKRoomInspection.ShowDataForm;
using YJKRoomInspection.ShowDataForm.FireProofDirectionCheck;
using YJKRoomInspection.Utiltiy;
using YArchitech.Controls;
using YJKInspectModelFunc;
using YJKInspectModelFunc.ControlLayer;
using YJKInspectModelFunc.ModelLayer;

namespace YJKRoomInspection.SpecialUse
{
	public class ResutlShow_CmdFrontRoomDoorsFilpCheck : IResutlShow<ICloseCurveAnalays>, IPackerResutlShow<ICloseCurveAnalays>, IBase<ICloseCurveAnalays>
	{
		public void ShowResults(List<ICloseCurveAnalays> input, params object[] otherValues)
		{
			List<Phase> list = new List<Phase>();
			this.m_doc = this.m_commandData.Application.ActiveUIDocument.Document;
			this.UiDoc = this.m_commandData.Application.ActiveUIDocument;
			PhaseArray phases = this.m_doc.Phases;
			for (int i = 0; i < phases.Size; i++)
			{
				list.Add(phases.get_Item(i));
			}
			BaseAreaFinder<BaseAreaPacker, FrontRoomPacker, BaseBoundarySegmentPacker, FrontRoomDoorPacker, BaseHostInRoomPacker> baseAreaFinder = new BaseAreaFinder<BaseAreaPacker, FrontRoomPacker, BaseBoundarySegmentPacker, FrontRoomDoorPacker, BaseHostInRoomPacker>();
			this.m_lstAreaPacker = baseAreaFinder.GetAllAreaPackersInDocument(this.m_doc, StaticRescource.m_SelectLevels, input, list, new List<BuiltInCategory>
			{
				(BuiltInCategory)(-2000120)
			}, false);
			int num = 0;
			FireProofDirectionForm fireProofDirectionForm = null;
			try
			{
				this.m_DoorDic = this.GetLevelsFromDoor(this.m_lstAreaPacker);
				foreach (Dictionary<int, List<FamilyInstance>> dictionary in this.m_DoorDic)
				{
					num += dictionary.Count;
				}
				if (num == 0)
				{
					YJKMessageBox.Show("您所检测的楼层中，防火门朝向全部符合规范要求");
				}
				else
				{
					new DoorDirectionViewLayerManger().DoViewLayer(this.m_commandData, StaticRescource.m_hWndRevit, this.UiDoc, this.m_lstAreaPacker);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
				if (fireProofDirectionForm != null)
				{
					fireProofDirectionForm.Close();
				}
			}
		}

		public List<Dictionary<int, List<FamilyInstance>>> GetLevelsFromDoor(List<BaseAreaPacker> m_lstAreaPackers)
		{
			bool flag = false;
			Dictionary<BaseDoorPacker, Room> dictionary = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<BaseDoorPacker, Room> dictionary2 = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<int, List<FamilyInstance>> dictionary3 = new Dictionary<int, List<FamilyInstance>>();
			Dictionary<int, Level> m_Level_index = new Dictionary<int, Level>();
			foreach (BaseAreaPacker tmpAreaPacker in m_lstAreaPackers)
			{
				int num = 0;
				dictionary = YJKRoomInspection.Utiltiy.Utility.GetOutDoorNum(tmpAreaPacker);
				dictionary2 = YJKRoomInspection.Utiltiy.Utility.GetFrontRoomAndStairDoor(tmpAreaPacker);
				using (Dictionary<BaseDoorPacker, Room>.Enumerator enumerator2 = dictionary.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						KeyValuePair<BaseDoorPacker, Room> kvp = enumerator2.Current;
						bool flag2 = YJKInspectModelFunc.Utility.IfDoorToRoom(kvp.Key, kvp.Value);
						if (flag2 && this.lstWrongDoor.Find((BaseDoorPacker n) => n.ThisDoor.Id == kvp.Key.ThisDoor.Id) == null)
						{
							this.lstWrongDoor.Add(kvp.Key);
						}
						num++;
					}
				}
				if (num <= 1)
				{
					flag = true;
				}
				using (Dictionary<BaseDoorPacker, Room>.Enumerator enumerator2 = dictionary2.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						KeyValuePair<BaseDoorPacker, Room> kvp = enumerator2.Current;
						bool flag2 = YJKInspectModelFunc.Utility.IfDoorToRoom(kvp.Key, kvp.Value);
						if (flag != flag2 && this.lstWrongDoor.Find((BaseDoorPacker n) => n.ThisDoor.Id == kvp.Key.ThisDoor.Id) == null)
						{
							this.lstWrongDoor.Add(kvp.Key);
						}
					}
				}
			}
			using (List<BaseDoorPacker>.Enumerator enumerator3 = this.lstWrongDoor.GetEnumerator())
			{
				while (enumerator3.MoveNext())
				{
					BaseDoorPacker tmpPacker = enumerator3.Current;
					Parameter parameter = tmpPacker.ThisDoor.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
					if (parameter != null)
					{
						Level level = this.m_doc.GetElement(parameter.AsElementId()) as Level;
						if (level != null && !dictionary3.Keys.Contains(level.Id.IntegerValue))
						{
							dictionary3.Add(level.Id.IntegerValue, new List<FamilyInstance>());
							m_Level_index.Add(level.Id.IntegerValue, level);
						}
						if (level != null && dictionary3[level.Id.IntegerValue].Find((FamilyInstance n) => n.Id == tmpPacker.ThisDoor.Id) == null)
						{
							dictionary3[level.Id.IntegerValue].Add(tmpPacker.ThisDoor);
						}
					}
				}
			}
			dictionary3 = (from k in dictionary3
			orderby m_Level_index[k.Key].Elevation
			select k).ToDictionary((KeyValuePair<int, List<FamilyInstance>> k) => k.Key, (KeyValuePair<int, List<FamilyInstance>> k) => k.Value);
			this.m_DoorDic.Add(dictionary3);
			return this.m_DoorDic;
		}

		private List<Dictionary<int, List<FamilyInstance>>> m_DoorDic = new List<Dictionary<int, List<FamilyInstance>>>();

		private List<BaseDoorPacker> lstWrongDoor = new List<BaseDoorPacker>();

		private List<BaseAreaPacker> m_lstAreaPacker = new List<BaseAreaPacker>();

		private ExternalCommandData m_commandData = StaticRescource.CmdData;

		private UIDocument UiDoc;

		private Document m_doc;
	}
}
